/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName NetworkDetecter.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model
{
	
	import com.adobe.nativeExtensions.Networkinfo.InterfaceAddress;
	import com.adobe.nativeExtensions.Networkinfo.NetworkInfo;
	import com.adobe.nativeExtensions.Networkinfo.NetworkInterface;
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	import com.gesturesoft.imultimax.model.vo.NetworkInfoVO;

	public class NetworkDetecter
	{
		
		public function NetworkDetecter()
		{
			
		}
		
		public function getNetworkInfo():void
		{
			
			var ntf:Vector.<NetworkInterface> = NetworkInfo.networkInfo.findInterfaces();
			var networkInfoVO:NetworkInfoVO = new NetworkInfoVO();
			var bool:Boolean = false;
			
			for each (var interfaceObj:NetworkInterface in ntf)
			{
				
				if(interfaceObj.active.toString() == "true")
				{
					
					networkInfoVO.interfaceName = interfaceObj.name;
					networkInfoVO.mTU = interfaceObj.mtu.toString();
					networkInfoVO.display_Name_Interface = interfaceObj.displayName;
					networkInfoVO.active = interfaceObj.active.toString();
					networkInfoVO.hardware_Address = interfaceObj.hardwareAddress;
					bool = true;
					for each(var address:InterfaceAddress in interfaceObj.addresses)
					{
						
						if(address.ipVersion == "IPV4")
						{
							
							networkInfoVO.address = address.address;
							networkInfoVO.broadcast_address = address.broadcast;
							networkInfoVO.ipversion = address.ipVersion;
							networkInfoVO.prefixlength = address.prefixLength.toString();
							
						}
						
					}
					
				}
				
			}
			
			if(bool)
			{
				
				try
				{
					
					updateDatabase(networkInfoVO);
					
				}
				catch(error:DataBaseError)
				{
					
					throw error;
					
				}
				
			}
			else
			{
				
			}
			
		}
		
		private function updateDatabase(value:NetworkInfoVO):void
		{
			
			var dataBaseManager:IDatabaseManager = new DataBaseManager();
			//dataBaseManager.prepareTable("networkinfotable");
			
			try
			{
				
				dataBaseManager.insertValues("INSERT INTO IM_IPHONE_NET_INFO(ini_interfaceName, ini_mTU, ini_display_Name_Interface, ini_active, ini_hardware_Address, ini_address, ini_broadcast_address, ini_ipversion, ini_prefixlength) VALUES('" +
					value.interfaceName + 
					"','" + value.mTU + 
					"','" + value.display_Name_Interface +
					"','" + value.active +
					"','" + value.hardware_Address +
					"','" + value.address +
					"','" + value.broadcast_address +
					"','" + value.ipversion +
					"','" + value.prefixlength + "')");
				
			}
			catch(error:DataBaseError)
			{
				
				throw error;
				
			}
			
		}
		
	}
	
}